﻿using Aurora.Discovery.HttpClient.Abstractions;
using Discovery.HttpClient.Consul.Options;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace Discovery.HttpClient.Consul;

/// <summary>
/// 扩展包
/// </summary>
public static class ConsulDiscoveryExtension
{
    public static void UseConsulDiscovery(this DiscoveryOptions options, Action<ConsulDiscoveryOptions> configure)
    {
        if (configure == null) throw new ArgumentNullException(nameof(configure));
        options.RegisterExtension(new ConsulDiscoveryOptionsExtension(configure));
    }


    // public static void AddConsulDiscovery(this IServiceCollection services, IConfiguration configuration)
    // {
    //     services.Configure<ConsulDiscoveryOptions>(configuration.GetSection("DiscoveryClient"));
    //     services.AddSingleton<ConsulDiscoveryClient>();
    //     services.AddScoped<DiscoveryHttpMessageHandler>();
    // }

    // public static IHttpClientBuilder AddHttpClientConsulDiscovery(this IHttpClientBuilder builder)
    // {
    //     builder.AddHttpMessageHandler<DiscoveryHttpMessageHandler>();
    //     return builder;
    // }


    // /// <summary>
    // /// 使用服务发现与注册
    // /// </summary>
    // /// <param name="app"></param>
    // /// <returns></returns>
    // public static WebApplication UseDiscovery(this WebApplication app)
    // {
    //     var lifetime = app.Services.GetService<IHostApplicationLifetime>();
    //     var discoveryClient = app.Services.GetService<ConsulDiscoveryClient>();
    //     lifetime?.ApplicationStarted.Register(() => { discoveryClient?.Start(); });
    //
    //     lifetime?.ApplicationStopping.Register(() => { discoveryClient?.Stop(); });
    //     return app;
    // }
}